package order.utils;

import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import common.model.order.Orders;
import order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.RoundingMode;

@Component
public class PayUtil {
    @Autowired
    private IOrderService orderService;
    //appid
    @Value("${alipay.appId}")
    public String appId;
    //私钥
    @Value("${alipay.appPrivateKey}")
    public String appPrivateKey;
    //字符编码形式
    private final String CHARSET = "UTF-8";
    //支付宝公钥
   @Value("${alipay.alipayPublicKey}")
    public String alipayPublicKey;
    //订单超时时间
    private String timeout = "10m";
    //沙箱接口路径
    private final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完之后异步跳转的地址
    @Value("${alipay.notifyUrl}")
    public String notifyUrl;
    //支付宝同步通知路径,付款完之后同步跳转的地址
    @Value("${alipay.returnUrl}")
    private String RETURN_URL;
    private AlipayClient alipayClient = null;
    public String sendRequestToAlipay(Orders order, String goodsName) throws AlipayApiException {
        //获得初始化AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, appId, appPrivateKey, FORMAT, CHARSET, alipayPublicKey, SIGN_TYPE);
        System.out.println("输入的内容为" +GATEWAY_URL+","+appId+","+appPrivateKey+","+FORMAT+","+CHARSET+","+alipayPublicKey+","+SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest ();
        alipayRequest.setReturnUrl(RETURN_URL + order.getId());//同步
        alipayRequest.setNotifyUrl(notifyUrl);//异步
        //商品描述
        String body = "zfb";
        String id = order.getOrderNo();
        Double money = order.getTotalAmount().setScale(2, RoundingMode.HALF_UP).doubleValue();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        bizContent.put("total_amount", money);
        bizContent.put("subject", goodsName);
        bizContent.put("body", body);
        bizContent.put("timeout_express", timeout);
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        alipayRequest.setBizContent(bizContent.toString());

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是" + result);
        System.out.println("输入的内容为2：" + alipayClient);
        return result;
    }
    public String query(String id) {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body = "";
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}

